package 和为K的子数组560;

import com.sun.java.swing.plaf.windows.WindowsTextAreaUI;

import javax.swing.plaf.IconUIResource;
import java.util.HashMap;

/**
 * @author 谢阳
 * @version 1.8.0_131
 */
class Solution {
    public static void main(String[] args) {
        int arr[] = {-1, 1, 0};
        int i = subarraySum(arr, 0);
        System.out.println(i);
    }

    public static int subarraySum(int[] nums, int k) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        hashMap.put(0, 1);
        int count = 0, sum = 0;
        for (int num : nums) {
            sum += num;
            if (hashMap.containsKey(sum - k)){
                count += hashMap.get(sum - k);
            }

            hashMap.put(sum,hashMap.getOrDefault(sum,0) + 1);
        }
        return count;
    }

}
